﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Windows;

//using SIMA.Server.Interface;
using SIMA.Core.Data.Collection;
using SIMA.Core.Data.Access;
using SIMA.Controls;
using SIMA.Core.Enums;
using SIMA.Entities;

namespace SIMA.Client.Auxiliars.MVVM
{
    public class AdminAreaMvvm : AccessClass<T_C_Area>
    {
        #region Consts

        private const int IDAREATABLE = 2;

        #endregion

        #region Fields

        SmartContainer<T_C_Area> areasSmart;
        List<T_C_Area> areas;
        SmartContainer<T_C_Estado> estadosSmart;
        List<T_C_Estado> estados;
        FilterType filtro;

        #endregion

        #region Properties

        public List<T_C_Area> Areas
        {
            get { return areas; }
            set
            {
                areas = value;
                this.RaisePropertyChanged("Areas");
            }
        }

        public List<T_C_Estado> Estados
        {
            get { return estados; }
            set
            {
                estados = value;
                this.RaisePropertyChanged("Estados");
            }
        }

        public FilterType Filtro
        {
            get
            {
                return filtro;
            }
            set
            {
                filtro = value;
                this.RaisePropertyChanged("Filtro");
                SelectFromStates(filtro);
            }
        }

        #endregion

        #region Events

        public event EventHandler CompleteAction;

        #endregion

        #region Constructor

        public AdminAreaMvvm()
        {
            Init();
        }

        #endregion

        #region Methods

        #region Private Methods

        private void Init()
        {
            DataAccess.SetConnection(SIMA.Core.Enums.AccessType.FromString, CADCONNECTIONSTRING);
            areasSmart = new SmartContainer<T_C_Area>(false);
            estadosSmart = new SmartContainer<T_C_Estado>(true);
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("Id_Tabla = ");
            strBuilder.Append(IDAREATABLE.ToString());
            estados = estadosSmart[strBuilder.ToString(), SIMA.Core.Enums.CommandType.CommandText];

            estadosSmart.CollectionChanged += new EventHandler(estadosSmart_CollectionChanged);

            areasSmart.Failed += new EventHandler<SIMA.Core.EventArgs.FailedEventArgs>(areasSmart_Failed);
            areasSmart.Updated += new EventHandler<SIMA.Core.EventArgs.UpdatedEventArgs>(areasSmart_Updated);
            areasSmart.Inserted += new EventHandler<SIMA.Core.EventArgs.InsertedEventArgs>(areasSmart_Inserted);
            areasSmart.Deleted += new EventHandler<SIMA.Core.EventArgs.DeletedEventArgs>(areasSmart_Deleted);
            areasSmart.CollectionChanged += new EventHandler(areasSmart_CollectionChanged);
            SelectFromStates(FilterType.Active);
            Filtro = FilterType.Active;
        }

        void areasSmart_Deleted(object sender, SIMA.Core.EventArgs.DeletedEventArgs e)
        {
            CompleteAction(this, new EventArgs());
            SIMAMessageBox.Show(e.InstantDeleted.ToLongTimeString(), "Eliminación realizada.", e.QueryString, SIMAMessageBoxButtons.OK, SIMAMessageBoxImage.Information);
            SelectFromStates(filtro);
        }

        void estadosSmart_CollectionChanged(object sender, EventArgs e)
        {
            CompleteAction(this, new EventArgs());
            areas = areasSmart.ToList();
        }

        void areasSmart_CollectionChanged(object sender, EventArgs e)
        {
            CompleteAction(this, new EventArgs());
            SelectAll();
        }

        void areasSmart_Inserted(object sender, SIMA.Core.EventArgs.InsertedEventArgs e)
        {
            CompleteAction(this, new EventArgs());
            SIMAMessageBox.Show(e.InstantInserted.ToLongTimeString(), "Se agregó nuevo registro", e.QueryString, SIMAMessageBoxButtons.OK, SIMAMessageBoxImage.Information);
            SelectFromStates(filtro);
        }

        void areasSmart_Updated(object sender, SIMA.Core.EventArgs.UpdatedEventArgs e)
        {
            CompleteAction(this, new EventArgs());
            SIMAMessageBox.Show(e.InstantUpdated.ToLongTimeString(), "Actualización realizada.", e.QueryString, SIMAMessageBoxButtons.OK, SIMAMessageBoxImage.Information);
            SelectFromStates(filtro);
        }

        void areasSmart_Failed(object sender, SIMA.Core.EventArgs.FailedEventArgs e)
        {
            if (CompleteAction != null)
            {
                CompleteAction(this, new EventArgs());
            }
            SIMAMessageBox.Show(e.InstantFailed.ToLongTimeString(), e.ExceptionOcurred.Message, e.QueryString, SIMAMessageBoxButtons.OK, SIMAMessageBoxImage.Error);
        }


        #endregion

        #region Public Methods

        public override void Register(T_C_Area item)
        {
            areasSmart.Add(item);
            SelectAll();
        }

        public override void Delete(T_C_Area item)
        {
            areasSmart.Remove(item);
            SelectAll();
        }

        public override void Update(T_C_Area itemToUpdate, T_C_Area itemUpdated)
        {
            areasSmart.Update(itemToUpdate, itemUpdated);
            SelectAll();
        }

        public override void SelectAll()
        {
            areasSmart.SelectAll();
            areas = areasSmart.ToList();
            if (CompleteAction != null)
            {
                CompleteAction(this, new EventArgs());
            }
        }

        public void SelectFromStates(FilterType filter)
        {
            switch (filter)
            {
                case FilterType.Active:
                    Areas = areasSmart["SelectActiveArea", CommandType.StoredProcedure];
                    break;
                case FilterType.Inactive:
                    Areas = areasSmart["SelectInactiveArea", CommandType.StoredProcedure];
                    break;
            }
        }

        #endregion

        #endregion

        #region Destructor

        ~AdminAreaMvvm()
        {
            this.Dispose();
        }

        #endregion

    }
}
